﻿using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class MapTargetMapSelect : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Map_MapTargetMapSelect;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<MapTargetMapSelectReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}


			//if (JsonConfig.Value.IsDebug)
			//{
			//	req.targetMapId = "600010";
			//}

			if (req.targetMapId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：targetMapId 参数为空）" });
				return;
			}
			var map = session.minecraftSessionInfo.map;
			//var sourceMap = CsvConfig.mapList.FirstOrDefault(m => m.Id == map.SourceMapId);
			var sourceMap = CsvConfig.GetMod_MapInfoById(map.SourceMapId);
			if (sourceMap == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：原地图信息不存在）" });
				return;
			}
			//var targetMap = CsvConfig.mapList.FirstOrDefault(m => m.Id == req.targetMapId);
			var targetMap = CsvConfig.GetMod_MapInfoById(req.targetMapId);
			if (targetMap == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：目标地图信息不存在）" });
				return;
			}
			if (sourceMap.Id == targetMap.Id)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：原地图Id与目标地图Id相同，不能调用此接口）" });
				return;
			}
			if (map.isMoving_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：正处于大地图界面的移动状态，不能调用此接口）" });
				return;
			}
			//判断targetMapId是否在可视范围内
			var IsWithinVisualField = map.IsWithinVisualField(targetMap.Id);
			if (!IsWithinVisualField)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：targetMapId不在可视范围内）" });
				return;
			}
			var mapRoute = map.GetRouteIdList(sourceMap, targetMap, true);
			var dist = map.GetDist(mapRoute);
			var secondForFast = Convert.ToInt32(dist / MinecraftConfig.MapTranslationalSpeed_fast);
			var secondForSlow = Convert.ToInt32(dist / MinecraftConfig.MapTranslationalSpeed_low);
			var needPhysicalStrengthValue = map.GetPhysicalStrengthByRoute(mapRoute);

			var resp = new MapTargetMapSelectResp
			{
				secondForFast = secondForFast,
				secondForSlow = secondForSlow,
				dist = dist,
				needPhysicalStrengthValue = needPhysicalStrengthValue
			};
			session.Send(defCommand, req, resp);
		}
	}
}
